<?php
class MedicineDefaultUsage extends ORDataObject {
	var $id = '';
	var $vi_usage_title = '';
	var $en_usage_title = '';
	var $usage_num = '';
	var $vi_usage_unit = '';
	var $en_usage_unit = '';
	var $dosage = '';
	var $time = '';
	var $vi_quantity_title = '';
	var $en_quantity_title = '';
	var $quantity_num = '';
	
	var $_table = 'medicine_default_usage';
	var $_key = 'id';
	var $_internalName = 'MedicineDefaultUsage';
	
	function MedicineDefaultUsage(){
		parent::ORDataObject();		
	}
	/**
	 * @author hdviet
	 * update for localize others
	 */
   
    /**
     * List all medicine default usage.
     *
     * @return Array of all default usage.
     */
	//TODO localization
    public function listAll() {
        $locale = L10nHelper::getLocale();
		$usageTitleLang = L10nHelper::changeField($locale ,"usage_title");
		$usageUnitLang = L10nHelper::changeField($locale ,"usage_unit");
		$quantityTitleLang = L10nHelper::changeField($locale ,"quantity_title");
		
        $ds = new Datasource_sql();
        $sql = array (
            'cols'      => "id, $usageTitleLang as usage_title, usage_num, $usageUnitLang as usage_unit, 
                            dosage, time, quantity_num, $quantityTitleLang as quantity_title",
            'from'      => "$this->_table",
            'orderby'   => "usage_title");
        $ds->setup($this->_db, $sql);
        return $ds->toArray();
    }
    
    /**
     * Get medicine default usage with usage_unit criteria.
     *
     * @param String $unitName The name of usage_unit
     */
    //TODO localization
    public function getMedicineUsageByUnit($unitName) {
    	$usageUnitLang = L10nHelper::changeField(L10nHelper::getLocale(),"usage_unit");
    	$ds = new Datasource_sql();
        $sql = array (
            'cols'      => "id, $usageTitleLang usage_title",
            'from'      => $this->_table,
            'where'     => "usage_unit = '$unitName'",
            'orderby'   => "usage_title");
        $ds->setup($this->_db, $sql);
        return $ds->toArray("id", "usage_title");
    }
    
    //TODO localization
    function setData($usageTitle, $enUsageTitle, $usageNum, $usageUnit, $enUsageUnit, 
    				$dosage, $time, $quantityNum, $quantityTitle, $enQuantityTitle) {
    	$data = array(
    		'vi_usage_title' => $usageTitle,
    		'en_usage_title' => $enUsageTitle,
    		'usage_num'	=> $usageNum,
    		'vi_usage_unit' => $usageUnit,
    		'en_usage_unit' => $enUsageUnit,
    		'dosage'	=> $dosage,
    		'time'	=> $time,
    		'quantity_num' => $quantityNum,
    		'vi_quantity_title' => $quantityTitle,
    		'en_quantity_title' => $enQuantityTitle
    	);
    	
    	$this->populateArray($data);
    }
}
?>